from unittest import TestLoader, TestCase, TextTestRunner
from pyduct.handlers import *
import pyduct.components
from pytest import GatewayAFactory
from argo.jdom import JdomParser, JsonNodeFactories
from java.io import StringReader
from java.util import Date
from pyson.gen import number, date, array
import pyduct.commonExtensions
from pyduct.validation import *

class ConsumeCookieMessage(PysonObject):
    msgType = string("ConsumeCookie")
    updatedValues = struct()

class SampleData(PysonObject):
    parameter0 = number(10)
    parameter1 = string("ABCD")
    parameter2 = boolean(1)
    parameter3 = string("NiceValue2")
    parameter4 = date(Date())
    parameter5 = array(string, ["V1", "V2", "V3"])

class TestComponents(TestCase):
    def testComposition(self): pass
#        class FakeProperty():
#            def name(self):
#                return "parameter0"
#
#        gateway = GatewayAFactory().newGateway(classOf("pytest.SampleStarterB"))
#        fragmentClass1 = CookieValidationBuilder.getFragmentClass(gateway, FakeProperty())
#        fragmentClass2 = MementoInfoForFieldBuilder.getFragmentClass(gateway, FakeProperty())
#
#        cls = PysonObjectMeta("test", (fragmentClass1, fragmentClass2), {})
#        print "cls.__mro__", cls.__mro__
#
#        print "C", cls()

    def testFieldsWithoutExtensions(self): pass

    #        gateway = GatewayAFactory().newGateway(classOf("pytest.SampleStarterB"))
    #        handler = JsonHandler()
    #        handler.fieldBuilders += pyduct.components.getBuilders()
    #
    #        cookieMsg = handler.getCookieMessage(gateway)
    #        self.assertEquals(6, len(cookieMsg.fields))

    def findField(self, fieldName, fields):
        for field in fields:
            if field.name == fieldName:
                return field
        self.fail("field %s not found" % fieldName)

    def testFieldsWithExtensions(self):
        cls = classOf("pytest.SampleStarterB")
        gateway = GatewayAFactory().newGateway(cls)
        handler = JsonHandler()
        handler.fieldBuilders += pyduct.components.getBuilders()

        builders = pyduct.validation.getBuilders() + \
        pyduct.metadata.getBuilders() + \
        pyduct.mementos.getBuilders() + \
        pyduct.lists.getBuilders()

        handler.extensionFragmentBuilders += builders

        cookieMsg = handler.getCookieMessage(gateway)
        self.assertEquals(6, len(cookieMsg.fields))
        field1 = self.findField("parameter1", cookieMsg.fields)
        self.assertEquals("xyz", field1.value)
        self.assertEquals("parameter1", field1.label)
        self.assertEquals("StringField", field1.otype)
        self.assertEquals("java.lang.String", field1.clazz)
        self.assertEquals("parameter1", field1.CommonMetaData.title)
        self.assertEquals(0, field1.MementoInfoForField.isParameter)
        self.assertEquals("Value1", field1.ListItems.data[0][0])
        self.assertEquals("Value1", field1.ListItems.data[0][1])
        self.assertEquals("Value2", field1.ListItems.data[1][0])
        self.assertEquals("Value2", field1.ListItems.data[1][1])
        self.assertEquals("Value3", field1.ListItems.data[2][0])
        self.assertEquals("Value3", field1.ListItems.data[2][1])
        self.assertEquals(1, field1.ListItems.editable)
        self.assertEquals(1, field1.ListItems.multiChoice)

#        cookieMsgBuilder = cookieMsg.createBuilder()
#        output = prettyFormat(cookieMsgBuilder)
#        print output

    def testUpdateField(self):
        msg = ConsumeCookieMessage()
        sample = SampleData()
        msg.updatedValues = sample
        rootNode = msg.createBuilder().build()

        #output = prettyFormat(msg.createBuilder())
        #print output

        gateway = GatewayAFactory().newGateway(classOf("pytest.SampleStarterB"))
        handler = JsonHandler()
        handler.fieldBuilders += pyduct.components.getBuilders()

        updatedNodesMap = rootNode.getFields().get(JsonNodeFactories.aJsonString("updatedValues")).getFields()
        updatedCookie = handler.updateCookie(gateway, updatedNodesMap)
        self.assertEquals(sample.parameter0, updatedCookie.getParameter0())
        self.assertEquals(sample.parameter1, updatedCookie.getParameter1())
        self.assertEquals(sample.parameter2, updatedCookie.getParameter2())
        self.assertEquals(sample.parameter3, updatedCookie.getParameter3().toString())
        self.assertEquals(sample.parameter4, updatedCookie.getParameter4())
        self.assertEquals(3, updatedCookie.getParameter5().size())
        self.assertEquals("V1", updatedCookie.getParameter5().get(0))
        self.assertEquals("V2", updatedCookie.getParameter5().get(1))
        self.assertEquals("V3", updatedCookie.getParameter5().get(2))

        pass

def runTests():
    suite = TestLoader().loadTestsFromTestCase(TestComponents)
    TextTestRunner(verbosity=2).run(suite)
